/*
 * uart.h
 *
 * UART driver.
 *
 * Copyright (C) 2022 Microwell, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: guoming<guoming@microwell.net>
 */

#ifndef __UART_H__
#define __UART_H__

#include "core.h"
/*=============================================================================
 *                               UART config
 *===========================================================================*/
/* 
 * NOTE: if use mt mode, user should switch between TX or RX mode manually.
 */ 

#define CONFIG_UART0_BAUD_RATE               (UART_BAUD_RATE_115200)

#define CONFIG_UART1_BAUD_RATE               (UART_BAUD_RATE_115200)

#define UART0_RX_BUF_LEN                     (20)
#define UART1_RX_BUF_LEN                     (20)

/*=============================================================================
 *                             UART config end
 *===========================================================================*/
typedef enum {
    UART_RX,
    UART_TX,
} UART_TRX;

typedef enum {
    FIRST_UART,
    SECOND_UART,
} UART_PORT;

extern idata uint8_t g_rx0_buf[UART0_RX_BUF_LEN];

#if (CONFIG_IC_TYPE == CHIP_SS888X)
typedef struct {
    uint8_t *ptr_tx;
    uint8_t u8tx_cnt;
    uint8_t u8rx_cnt;
} UART1_STRUCT;

extern uint8_t g_rx1_buf[UART1_RX_BUF_LEN];

#define UART1_RESET()                              SET_BIT(CLKCON2, UART1_RST)
#define UART1_CLK_ENABLE()                         SET_BIT(CLKCON2, UART1_CE)
#define UART1_TX_ENABLE()                          SET_BIT(U1CON0, UART1_TXEN)
#define UART1_TX_DISABLE()                         CLEAR_BIT(U1CON0, UART1_TXEN)
#define UART1_RX_ENABLE()                          SET_BIT(U1CON0, UART1_RXEN)
#define UART1_RX_DISABLE()                         CLEAR_BIT(U1CON0, UART1_RXEN)
#define UART1_TX_HALF_ISR_ENABLE()                 SET_BIT(U1CON1, UART1_TXFHEIE)
#define UART1_TX_HALF_ISR_DISABLE()                CLEAR_BIT(U1CON1, UART1_TXFHEIE)
#define UART1_RX_LEVEL_ISR_ENABLE()                SET_BIT(U1CON1, UART1_RXFHFIE)
#define UART1_RX_LEVEL_ISR_DISABLE()               CLEAR_BIT(U1CON1, UART1_RXFHFIE)
#define UART1_RX_IDLE_ISR_ENABLE()                 SET_BIT(U1CON1, UART1_RXIDLEIE)
#define UART1_RX_IDLE_ISR_DISABLE()                CLEAR_BIT(U1CON1, UART1_RXIDLEIE)
#define UART1_ISR_ENABLE()                         _EUART1_INTERRUPT(SET)
#define UART1_ISR_DISABLE()                        _EUART1_INTERRUPT(RESET)
#define UART1_RX_IDLE()                            (!!READ_BIT(U1CON1, UART1_RXIDLE))
#define UART1_TX_FIFO_FULL()                       (!!READ_BIT(U1STAT, UART1_TXFF))
#define UART1_TX_FIFO_EMPTY()                      (!!READ_BIT(U1STAT, UART1_TXFE))
#define UART1_TX_FIFO_HALF()                       (!!READ_BIT(U1STAT, UART1_TXFHE))
#define UART1_RX_ERROR()                           (!!READ_BIT(U1STAT, UART1_RER))
#define UART1_RX_FIFO_OVER()                       (!!READ_BIT(U1STAT, UART1_RXFO))
#define UART1_RX_FIFO_NOEMPTY()                    (!!READ_BIT(U1STAT, UART1_RXFNE))
#define UART1_RX_FIFO_HALF()                       (!!READ_BIT(U1STAT, UART1_RXFHF))
#endif

#if (UART_CHANNEL_MODE > 0)

void uart_init(void);
void uart_exit(void);
void uart_suspend(void);
void uart_pinmux_get(bool is_tx, uint8_t uart_num);

void clear_rx0_buff(void);
uint8_t rx0_buf_get(void);
void uart0_putc(uint8_t c);
void uart0_send(uint8_t *buf, uint8_t length);
void uart0_tx_mfp_enable(void);
void uart0_rx_mfp_enable(void);
void uart0_tx_mfp_disable(void);
void uart0_rx_mfp_disable(void);
void uart0_pinmux_put(void);
void uart0_pinmux_get(bool is_tx);
uint8_t get_rx0_data_num(void);

#if (CONFIG_IC_TYPE == CHIP_SS888X)
void clear_rx1_buff(void);
void uart1_putc(uint8_t c);
void uart1_send(uint8_t *buf, uint8_t length);
void uart1_tx_mfp_enable(void);
void uart1_rx_mfp_enable(void);
void uart1_tx_mfp_disable(void);
void uart1_rx_mfp_disable(void);
void uart1_pinmux_put(void);
void uart1_pinmux_get(bool is_tx);

void uart_putc(uint8_t uart_port, uint8_t c);
void uart_send(uint8_t uart_port, uint8_t *buf, uint8_t length);
#endif
#else
#define uart_init()
#define uart_exit()
#define uart_suspend()
#define uart_pinmux_put()

#define clear_rx0_buff()
#define uart0_putc(c)
#define uart0_send(buf, length)
#define uart0_tx_mfp_enable()
#define uart0_rx_mfp_enable()
#define uart0_tx_mfp_disable()
#define uart0_rx_mfp_disable()
#define uart0_pinmux_get(as_tx)
#define uart0_pinmux_put()

#define clear_rx1_buff()
#define uart1_putc(c)
#define uart1_send(buf, length)
#define uart1_tx_mfp_enable()
#define uart1_rx_mfp_enable()
#define uart1_tx_mfp_disable()
#define uart1_rx_mfp_disable()
#define uart1_pinmux_get(as_tx)
#define uart1_pinmux_put()

#define uart_putc(uart_port, c)
#define uart_send(uart_port, buf, length)

#endif
#endif /* __UART_H__ */
